<!DOCTYPE html>
<html lang="en">
<head>
    <title>检验项目-精工云MES系统移动端</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <link href="/JG/Content/jquery/weui/style/weui.min.css" rel="stylesheet" />
    <link href="/JG/Content/jquery/jquery-weui/css/jquery-weui.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="/JG/Content/css/bootstrap.min.css">
    <link href="/JG/Content/css/Global.css" rel="stylesheet" />
    <link href="/JG/Content/jquery/jquery-weui/css/icon.css" rel="stylesheet" />
    <link rel="stylesheet" href="/JG/Content/css/MobileCheckoutProject.css?v=1">

    <script src="/JG/Content/jquery/jquery-3.2.1.min.js"></script>
    <script src="/JG/Content/LocalConfigs.js"></script>
    <script src="/JG/Content/LocalUserInfo.js"></script>
    <script src="/JG/Content/Utils.js?v=1"></script>
</head>
<body>
    <div id="ContentContainer">
        <!--header部分-->
        <header class="header">
            <div class="empty">
                <span id="stations"></span>
            </div>
            <div class="weui-flex">
                <div class="weui-flex__item" style="position:absolute;">
                <span style="font-size:16px;padding-left: 10px;" class="icon icon-109 f-white" @click="BackTosj">
                    返回
                </span>
                </div>
                <div class="weui-flex__item" style="text-align: center;">检验项目</div>
                <div class="weui-flex__item" style="position:absolute;right:10px;" @click="SaveBtn">
                    <span><img src="/JG/Content/images/save.png" alt="" style="width:20px;"></span>
                </div>
            </div>
        </header>

        <!--解决浮动空内容div-->
        <div class="empty_div"></div>

        <!--主体部分-->
        <div id="MainPage" v-cloak>
            <div class="tree_form">
                <!--无限层级菜单部分-->
                <!--<div id="menuTree" class="menuTree"></div>-->


                <div class="menuTree">
                    <div class="big_title">
                        <span @click="GetRootID(item,index)" v-for="(item,index) in CheckoutClassifyData" :name=item.JGMES_ZLGL_JCXMFL_ID>{{item.JCXMFL_FLMC}}</span>
                    </div>
                    <!--树形结构数据-->
                    <div class="all_detail">
                        <div id="detail_list" class="detail_list"></div>
                        <!--<div id="detail_item" class="detail_item"></div>-->
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

<script src="/JG/Content/jquery/jquery-weui/js/jquery-weui.min.js"></script>
<script src="/JG/Content/bootstrap.min.js"></script>
<script src="/JG/Content/vue-v2.34.js"></script>
<script src="/JG/Content/Common.js"></script>
<script type="text/javascript" src="/JG/Content/TierPullDown.js"></script>

<script type="text/javascript">

    var vmPage = new Vue({
        el:"#ContentContainer",
        data:{
            FormData:{
                MasterID:$.getUrlParam("MasterID"),     //将传过来的检验方案ID转为符合getUrlParam()方法里面的正则
               // RootId:"",     //检验项目分类根目录id
                FirstRootId:"",     //第一个检验项目分类根目录id
                Content:"",
            },
            CheckoutClassifyData:[], //检验项目分类根目录集合（多层级）
            CheckoutProjectData:[],    //检验项目集合（多层级）
            CheckoutItemData:[],      //检测项集合
            UpId:"",//上次标签的id
        },
        mounted:function(){
            var currSelf = this;
            currSelf.TreeFormData();   //初始化
 
        },

        methods:{
            TreeFormData:function(){
                var currSelf = this;
                if(currSelf.FormData.MasterID){
                    //获取检验项目分类的根目录数据
                    $.showLoading();
                    $.ajax({
                        type: "post",
                        async:false,
                        url: LocalConfig.SrvPath + "/jgmes/jgmesFirstInspectionAction!getInspectionItemRootClassify.action",
                        data: {
                            "userCode": LocalUserInfo.GetUserInfo().UserCode,
                            "mac": LocalUserInfo.GetUserInfo().Mac,
                            "jybzID":currSelf.FormData.MasterID,     //主键ID
                        },
                        dataType: "json",
                        success:function(result){
                        	var jsonStrDetail="";
                        	var titleOne="";
                            var res = ReturnData(result);
                            //console.log(res);
                            if(res.IsSuccess){
                                if(res.Data.length>0){
                                    currSelf.CheckoutClassifyData = res.Data;
//                                  console.log(currSelf.CheckoutClassifyData);
                                    //循环遍历res.Data获取跟标签
                                    titleOne+="{"
                                    //alert(currSelf.FormData.FirstRootId);
                                    for(var i=0;i<res.Data.length;i++){
//                              		var titleOneStr="{\""+res.Data[i].JGMES_ZLGL_JCXMFL_ID+"\":\"\"}";
//	                                  	titleOne.push(titleOneStr);
										if((i+1)==res.Data.length){
		                                  	titleOne+='"'+res.Data[i].JGMES_ZLGL_JCXMFL_ID+'":""';
		                                  }else{
		                                  	titleOne+='"'+res.Data[i].JGMES_ZLGL_JCXMFL_ID+'":"",'
		                                  }
	                                    }
                                    titleOne+='}';
                                    if(!localStorage.getItem("jsonStrDetail")){
                                    	localStorage.setItem("jsonStrDetail",titleOne);
                                    }
                                    currSelf.FormData.FirstRootId = res.Data[0].JGMES_ZLGL_JCXMFL_ID;
                                    if(currSelf.FormData.FirstRootId){
                                        currSelf.InitializeCheckout();    //初始化
                                        //循环点击根目录数据显示不同样式
                                        currSelf.$nextTick(function () {   //$nextTick方法是进行获取数据后，对更新后的hmtl做操作
                                            $(".big_title>span:first-child").addClass("active_span");
                                            $(".big_title>span").each(function (index) {
                                                $(this).on("click", function () {
                                                    $(".big_title>span").eq(index).addClass("active_span").siblings(".big_title>span").removeClass("active_span");
                                                    // $(".detail_list>div").hide();
                                                    // $(".detail_list>div").eq(index).show();
                                                })
                                            })
                                        });
                                    }
                                }else {
                                    var showMsg = "此检验方案没有相关检测分类项!";
                                    $.toptip(showMsg, "warning");
                                }
                            }else {
                                $.toptip(res.message, "error");
                            }
                           // jsonStrDetail=titleOne;
                           // console.log(jsonStrDetail);
                           currSelf.SetJsonStrDetail(res.Data[0].JGMES_ZLGL_JCXMFL_ID);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $.alert("请求失败" + errorThrown);
                        },
                        complete: function () {
                            //当数据加载完成隐藏加载弹窗
                            $.hideLoading();
                        }
                    });
                }

            },

            //初始化检验分类项和分类项目方法
            InitializeCheckout:function(){
                var currSelf = this;
                currSelf.CheckoutProjectData =[];
                str="";
                //获取检验项目明细接口
                $.showLoading();
                $.ajax({
                    type: "post",
                    async:false,
                    url: LocalConfig.SrvPath + "/jgmes/jgmesFirstInspectionAction!getEffectiveProductStandardChild.action",
                    data: {
                        "userCode": LocalUserInfo.GetUserInfo().UserCode,
                        "mac": LocalUserInfo.GetUserInfo().Mac,
                        "jybzID":currSelf.FormData.MasterID,     //首检主键ID
                        "rootId":currSelf.FormData.FirstRootId,    //检验项目分类ID
                    },
                    dataType: "json",
                    success:function(result){
                        var res = ReturnData(result);
                       // console.log(res);
                        if(res.IsSuccess){
                            if(res.Data.length>0){
                                currSelf.CheckoutProjectData=res.Data;
                                //添加无级树
                               // console.log(currSelf.CheckoutProjectData)
                                // document.getElementById("detail_list").innerHTML = forTree(currSelf.CheckoutProjectData);
                                var StrDetail=$.parseJSON(localStorage.getItem("jsonStrDetail"));
//                              console.log(currSelf.FormData.FirstRootId);
                                if(!StrDetail||StrDetail[currSelf.FormData.FirstRootId]==""){
	                                var strJson="{";
	                         		for(var i=0;i<res.Data.length;i++){
	                         			var root=res.Data[i];
	                                 	for(var j=0;j<root.childTreeDto.length;j++){
	                                 		var jroot=root.childTreeDto[j];
	                                 		if(jroot.data.length>0){
	                                 			for(var n=0;n<jroot.data.length;n++){
		                                 			if(n+1==jroot.data.length){
			                                 			strJson+='"'+jroot.data[n].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":""';
		                                 			}else{
		                                 				strJson+='"'+jroot.data[n].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":"",';
		                                 			}
	                                 			}
	                                 		}else{
	                                 			if(j+1==root.childTreeDto.length){
		                                 			strJson+='"'+jroot.childTreeDto[0].data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":""';
		                                 			//console.log(jroot.data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID)
	                                 			}else{
	                                 				strJson+='"'+jroot.childTreeDto[0].data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":"",';
	                                 			}
	                                 		}
//	                                 		for(var n=0;n<jroot.data.length;n++){
//	                                 			if(n+1==jroot.data.length){
//		                                 			strJson+='"'+jroot.data[n].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":""';
//	                                 			}else{
//	                                 				strJson+='"'+jroot.data[n].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":"",';
//	                                 			}
//	                                 		}
//	                                 		if(jroot.data.length>0){
//	                                 			if(j+1==root.childTreeDto.length){
//		                                 			strJson+='"'+jroot.data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":""';
//		                                 			//console.log(jroot.data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID)
//	                                 			}else{
//	                                 				strJson+='"'+jroot.data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":"",';
//	                                 			}
//	                                 		}else{
//	                                 			if(j+1==root.childTreeDto.length){
//		                                 			strJson+='"'+jroot.childTreeDto[0].data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":""';
//		                                 			//console.log(jroot.data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID)
//	                                 			}else{
//	                                 				strJson+='"'+jroot.childTreeDto[0].data[0].productInsStandardChildList[0].values.JGMES_ZLGL_CPJYBZZB_ID+'":"",';
//	                                 			}
//	                                 		}
	                                 	}
	                         		}
	                         		strJson+="}";
	                         		var jsonzb=$.parseJSON(strJson);
								   	StrDetail[currSelf.FormData.FirstRootId]=jsonzb;
									localStorage.setItem("jsonStrDetail",JSON.stringify(StrDetail));
                         		}
                                

                                
                                $("#detail_list").html(forTree(currSelf.CheckoutProjectData));
                                currSelf.FormData.Content= str;
                            }else {
                                var showMsg = "此检测分类项无相关检验项目!";
                                $.toptip(showMsg, "warning");
                            }
                        }else {
                            $.toptip(res.message, "error");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.alert("请求失败" + errorThrown);
                    },
                    complete: function () {
                        //当数据加载完成隐藏加载弹窗
                        $.hideLoading();
                    }
                });

            },

            //获取当前点击检验项目分类名称方法
            GetRootID:function(val){
//          	console.log($('.big_title span.active_span').attr('name'));
               	var a=$('.big_title span.active_span').attr('name');
               	UpId=a;
                var currSelf = this;
                currSelf.FormData.FirstRootId = val.JGMES_ZLGL_JCXMFL_ID;
                currSelf.SavejsonStrDetail(UpId );
                currSelf.InitializeCheckout();     //调用
                currSelf.SetJsonStrDetail(currSelf.FormData.FirstRootId);
            },
            //保存数据到localStorage
            SavejsonStrDetail:function(o){
            	var jsonStrDetail=localStorage.getItem("jsonStrDetail");
            	var StrDetail=$.parseJSON(jsonStrDetail);
//          	console.log(StrDetail[o]);
            	for(var p in StrDetail[o]){
            		var jsonArray=new Array();
            		if(StrDetail[o][p]){
            			jsonArray.push(StrDetail[o][p][0]);
            		}else{
            			jsonArray.push("");
            		}
//          		console.log(p);
					var radioVal=$('.'+p+'').find("input[type=radio]:checked").val();
					var inputVal=$('.'+p+' input[name=jyztext]').val();
					if(radioVal){
						jsonArray.push(radioVal);
					}
					if(inputVal){
						jsonArray.push(inputVal);
					}
					StrDetail[o][p]=jsonArray;
				}
            	localStorage.setItem("jsonStrDetail",JSON.stringify(StrDetail));
            },
            //赋值页面数据
            SetJsonStrDetail:function(o){
            	var jsonStrDetail=localStorage.getItem("jsonStrDetail");
            	var StrDetail=$.parseJSON(jsonStrDetail);
            	for(var p in StrDetail[o]){
					if(StrDetail[o][p][1]){
						$("input[name='"+p+"'][value='"+StrDetail[o][p][1]+"']").attr("checked",true); 
					}
					if(StrDetail[o][p][2]){
						$('.'+p+' input[name=jyztext]').val(StrDetail[o][p][2]);
					}
				}
            	//localStorage.setItem("jsonStrDetail",JSON.stringify(StrDetail));
            },
           //返回上一页
           BackTosj:function(){
           	var currSelf = this;
			var a=$('.big_title span.active_span').attr('name');
			currSelf.SavejsonStrDetail(a);
           	location.href='/JG/Quality/MobileFirstItemInspection.html'
           }
            ,
            //保存按钮方法
            SaveBtn:function() {
            			var currSelf = this;
            			var a=$('.big_title span.active_span').attr('name');
            			currSelf.SavejsonStrDetail(a);
                        $.showLoading();
                        var primarytab=$.parseJSON(localStorage.getItem("jsonStr"));
                        //delete primarytab["JGMES_ZLGL_SJJY_ID"];
						if(primarytab.SJJY_SJR==""){
        					msg = '主表表送检人不能为空!';
							$.toptip(msg, "warning");
        					return $.hideLoading();;
						}
						if(primarytab.SJJY_NO_CODE==""){
        					msg = '主表表是否合格不能为空!';
							$.toptip(msg, "warning");
        					return $.hideLoading();;
						}
						
						primarytab.SJJY_NO_CODE=="合格"?primarytab.SJJY_NO_CODE=1:primarytab.SJJY_NO_CODE=0;
                 		//格式化子表数据
                        var zbtable=$.parseJSON(localStorage.getItem("jsonStrDetail"));
                       	var jsonTab=new Array();
	                        for(var b in zbtable){
	                        	for(var c in zbtable[b]){
                        			var ObjectJson={};
	                        		ObjectJson["JGMES_ZLGL_CPJYBZZB_ID"]=c;
	                        		ObjectJson["JGMES_ZLGL_SJJYZB_ID"]=zbtable[b][c][0];
                                    ObjectJson["SJJYZB_JYFAZBZJID"]=c;
	                        			if(zbtable[b][c][1]=="合格"){
	                        				ObjectJson["SJJYZB_HG_CODE"]=1;
	                        			}else if(zbtable[b][c][1]=="不合格"){
	                        				ObjectJson["SJJYZB_HG_CODE"]=0;
	                        			}else{
	                        				$.hideLoading();
                        					msg = '子表是否合格不能为空!';
											$.toptip(msg, "warning");
                        					return;
	                        			}
	                        			if(zbtable[b][c].length>2){
	                        				if(zbtable[b][c][2]==""){
	                        					$.hideLoading();
	                        					msg = '子表检验值不能为空!';
												$.toptip(msg, "warning");
	                        					return;
	                        				}
	                        				ObjectJson["SJJYZB_JYZ"]=zbtable[b][c][2];
	                        			}
	                        		jsonTab.push(ObjectJson);
	                        	}
                       	}
                        $.ajax({
                            type: "post",
                            //async: false,
                            url: LocalConfig.SrvPath + "/jgmes/jgmesFirstInspectionAction!doSaveFirstInspection.action",
                            data: {
                                "userCode": LocalUserInfo.GetUserInfo().UserCode,
                                "mac": LocalUserInfo.GetUserInfo().Mac,
                                "jsonStr":JSON.stringify(primarytab) ,       //主表
                                "jsonStrDetail": JSON.stringify(jsonTab)   //子表
                            },
                            dataType: "json",
                            success: function (result) {
                                var retData = ReturnData(result);
                                if (retData.IsSuccess) {
                                	localStorage.removeItem('jsonStr');
									localStorage.removeItem('jsonStrDetail');
									msg = "保存成功!";
									$.toptip(msg, "success");
									window.location.href = "/JG/Quality/MobileFirstCheckout.html";
                                } else {
                                    msg = "保存失败!";
                                    $.toptip(msg, "error");
                                }
                            },
                            error: function (xhr, status, errorThrow) {
                                console.error(status);
                                $.hideLoading();
                                //$.alert("批量保存数据失败！");
                            },
                            complete: function () {
                                $.hideLoading();
                            }
                        });
               
                }
        },
    });
</script>
</html>